package com.muse.appManage.service;

import com.google.common.collect.ImmutableMap;
import com.muse.constant.Status;
import com.muse.entity.EyebrowShape;
import com.muse.entity.Result;
import com.muse.entity.UserInfo;
import com.muse.model.EyebrowShapeListParam;
import com.muse.model.UserInfoVo;
import com.muse.sqlutils.And;
import com.muse.sqlutils.SqlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.muse.sqlutils.SqlBuilder.*;
import static com.muse.sqlutils.SqlBuilder.select;

/**
 * Created with IntelliJ IDEA.
 * User: lee
 * Date: 2021/4/27
 * Time: 5:35 下午
 * Description:
 */
@Service
@Slf4j
public class ManageEyebrowShapeService {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public Result create(EyebrowShape eyebrowShape) {
        if (check(eyebrowShape) != null) {
            return check(eyebrowShape);
        }
        try {
            SqlBuilder.insert("t_eyebrow_shape", eyebrowShape, jdbcTemplate);
            return Result.success();
        } catch (Exception e) {
            log.error("create eyebrowShape error", e);
            return Result.failure();
        }
    }

    public Result<List<EyebrowShape>> list(EyebrowShapeListParam param, Integer page, Integer pageSize) {
        And and = new And();
        and.add(eq("1", 1));
        if (param.getIsPay() != null) {
            and.add(eq("is_pay", param.getIsPay()));
        }
        long total = select("t_eyebrow_shape").where(and).queryForCount(jdbcTemplate);
        List<EyebrowShape> vos = select("t_eyebrow_shape").where(and)
                .order("create_time desc")
                .limit((page - 1) * pageSize, pageSize)
                .queryForList(jdbcTemplate, EyebrowShape.class);
        vos.forEach(v -> {
            v.setStatusName(Status.getStatusByValue(v.getStatus()).getName());
        });
        return Result.success(vos).pagination(new Result.Pagination(page, pageSize, (int) total));
    }

    public Result updateStatus(Integer id, Integer status) {
        SqlBuilder.update("t_eyebrow_shape", ImmutableMap.of("status", status)).where(eq("id", id)).execute(jdbcTemplate);
        return Result.success();
    }

    public Result update(EyebrowShape eyebrowShape) {
        if (check(eyebrowShape) != null) {
            return check(eyebrowShape);
        }
        SqlBuilder.update("t_eyebrow_shape", eyebrowShape, eq("id", eyebrowShape.getId()), jdbcTemplate);
        return Result.success();
    }

    private Result check(EyebrowShape eyebrowShape) {
        if (StringUtils.isEmpty(eyebrowShape.getLeftEyebrow())) {
            return Result.failure("请上传左眉图片");
        }
        if (StringUtils.isEmpty(eyebrowShape.getRightEyebrow())) {
            return Result.failure("请上传右眉图片");
        }
        if (StringUtils.isEmpty(eyebrowShape.getTitle())) {
            return Result.failure("请上传标题");
        }
        if (eyebrowShape.getIsPay() == null) {
            return Result.failure("请选择收费状态");
        }
        return null;
    }


}
